package dataAccessObjects;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

import entidades.Cita;

public class CitaDAO {

    EntityManager em;

    public void CreateCita(EntityManagerFactory emf, Cita cita) {
        try {
            em = emf.createEntityManager();
            em.getTransaction().begin();
            em.persist(cita);
            em.flush();
            em.getTransaction().commit();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
        }
    }

    public Cita ReadCita(EntityManagerFactory emf, int id){
        em = emf.createEntityManager();
        Cita cita = new Cita();
        em.getTransaction().begin();
        try{
            Query query = em.createQuery("SELECT cita FROM Cita cita WHERE cita.id = "+id);
            cita = (Cita)query.getSingleResult();
            em.getTransaction().commit();
            }
        catch(Exception e){
            System.out.println(e.getMessage());
                }
        finally{
            em.close();
        }
        return cita;
    }

    public void UpdateCita(EntityManagerFactory emf, int id, Cita cita) {
        try {
            em = emf.createEntityManager();
            em.getTransaction().begin();
            Cita citaNuevo = em.find(Cita.class, id);
           // citaNuevo.setmMedico(id);
           // citaNuevo.setmPaciente(cita.getmPaciente());
           // citaNuevo.setmConsultorio(consultorio.ismConsultorio());
            em.merge(citaNuevo);
            em.getTransaction().commit();


        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
        }

    }

    public Cita DeleteCita(EntityManagerFactory emf, int id) {
        Cita citaABorrar = new Cita();
        Cita cita = ReadCita(emf, id);
        try {
            em = emf.createEntityManager();
            em.getTransaction().begin();
            citaABorrar = em.find(Cita.class, id);
            em.remove(citaABorrar);
            em.getTransaction().commit();
        } finally {
            em.close();

        }
        return cita;
    }

    public ArrayList<Cita> getAllByUsuarioId(EntityManagerFactory emf, int id) {
        ArrayList<Cita> listaCitas = new ArrayList<Cita>();
        em.getTransaction().begin();

        try {
            Query query = em.createQuery("SELECT cita FROM Cita cita");
            List lst = query.getResultList();
            Iterator it = lst.iterator();
            while (it.hasNext()) {
                Cita cita = (Cita) it.next();
                listaCitas.add(cita);
            }
            em.getTransaction().commit();

        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            em.close();
        }
        return listaCitas;
    }
}
